/**
 * Author:yuiop
 * Date:2023/2/10 13:45
 */
package com.devops.admin.temporal.workflow.impl;

import com.devops.admin.temporal.activity.DOSProjectVersionTimeoutActivity;
import com.devops.admin.temporal.workflow.DOSProjectVersionTimeOutWorkflow;
import com.devops.constant.DevOpsConstant;
import com.devops.constant.TimeOutOperationEnum;
import io.temporal.activity.ActivityOptions;
import io.temporal.spring.boot.WorkflowImpl;
import io.temporal.workflow.Workflow;

import java.time.Duration;

@WorkflowImpl(taskQueues = DevOpsConstant.PROJECT_VERSION_WORKFLOW_TASK_QUEUE)
public class DOSProjectVersionTimeOutWorkflowImpl implements DOSProjectVersionTimeOutWorkflow {
    ActivityOptions options = ActivityOptions.newBuilder()
            .setScheduleToCloseTimeout(Duration.ofSeconds(60))
            .build();

    // ActivityStubs enable calls to Activities as if they are local methods, but actually perform an RPC.
    private final DOSProjectVersionTimeoutActivity projectVersionTimeoutActivity = Workflow.newActivityStub(DOSProjectVersionTimeoutActivity.class, options);
    @Override
    public void checkTimeOutProjectVersion(Integer projectVersionId) {
        Workflow.sleep(Duration.ofHours(1));
        boolean isOpen = projectVersionTimeoutActivity.dealProjectVersionTimeout(projectVersionId, TimeOutOperationEnum.NOTICE);
        if (!isOpen) {
            return;
        }
        Workflow.sleep(Duration.ofHours(23));
        projectVersionTimeoutActivity.dealProjectVersionTimeout(projectVersionId, TimeOutOperationEnum.CLOSE);
    }
}
